// 用express框架
const express = require("express");
const cors = require("cors");

const server = express();

server.use(cors());
// 1. 静态页面托管
server.use(express.static("public"));
//    localhost:3000/index.html可以访问public下的index.html文件
// 2. 写接口
server.use(express.urlencoded());
server.post("/post", (req, res) => {
    // console.log(req.body);
    const {
        a,
        b
    } = req.body;
    console.log(a, b);
    if (!a || !b) {
        res.status(404).end({
            code: 400,
            msg: "a,b null"
        });
        return;
    }
    const data = JSON.stringify(req.body);
    res.end(data);
})


server.use(express.json());
server.post("/postJSON", (req, res) => {
    const jsonData = req.body;
    console.log(jsonData);
    res.json(jsonData);
    // res.end(JSON.stringify(req.body));
})

const multer = require("multer");
const upload = multer({
    dest: 'uploads/'
});
server.post("/admin/article_publish", upload.single('cover'), (req, res) => {
    const file = req.file;
    const info = req.body;
    const obj = {
        file: file,
        info: info
    }
    console.log(obj);
    res.send(obj);

})

//    在后端实现三个接口，分别来处理在index.html中发出的post请求：
//    只需要在后端接收到参数即可

server.listen(3000, () => {
    console.log('ok');
})